const xss = require("xss");
const { exec } = require("../db/mysql");

const paperTypeList = () => {
  let sql = `select * from grade`;
  return exec(sql);
};
const addVerifyPaper = (data = {}) => {
    console.log(data);
  let paper_title = data.paper_title;
  let paper_name = data.paper_name;
  let grade_id = data.grade_id;
  let paper_time = data.paper_time;
  let user_id = data.user_id;
  let paper_url = data.paper_url;
  const sql = `insert into verify_paper(paper_title,paper_name,grade_id,paper_time,user_id,paper_url) 
  values('${paper_title}','${paper_name}','${grade_id}','${paper_time}','${user_id}','${paper_url}');`;
  // console.log(sql);
  return exec(sql).then((insertData) => {
    if (insertData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const updatePaperType = (id, paperData = {}) => {
  const grade_name = xss(paperData.grade_name);

  const sql = `update grade set grade_name='${grade_name}' where grade_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const delPaperType = (id) => {
  console.log(id);
  const Selsql = `select paper.grade_id from paper where grade_id=${id}`;
  return exec(Selsql).then((rows) => {
    if (rows[0] === undefined) {
      const sql = `delete from grade where grade_id=${id}`;
      return exec(sql).then((delData) => {
        if (delData.affectedRows > 0) {
          return true;
        }
        return false;
      });
    } else {
      return {
        code: -1,
        msg: "无法删除该类型",
      };
    }
  });
};
//--------------------------------------------------------------------------

module.exports = {
  paperTypeList,
  addVerifyPaper,
  updatePaperType,
  delPaperType,
  //-------------
};
